package config

import (
	"github.com/hwcer/cosgo/random"
	"github.com/hwcer/yyds"
)

const (
	ItemRandomProcessName = "itemRandomProcess"
)

type ItemRandomProcess struct {
	Bong  map[int32]*ItemBongRandom
	Packs map[int32]*random.Random
	Group map[int32]*random.Random
}

type ItemBongRandom struct {
	random.Random
	Type int32
}

func (this *ItemBongRandom) Roll() []int32 {
	if this.Type == 1 {
		return this.Random.Probability(1000)
	} else if this.Type == 2 {
		r := this.Random.Roll()
		return []int32{r}
	} else {
		return nil
	}
}

//type itemPacksProcess map[int32]*random.Random
//type itemGroupProcess map[int32]*random.Random

// 道具类型分析
func init() {
	yyds.Config.Register(&items{})
}

func GetItemRandom() *ItemRandomProcess {
	i := yyds.Config.Process.Get(ItemRandomProcessName)
	if i == nil {
		return nil
	}
	return i.(*ItemRandomProcess)
}

type items struct {
	HandleDefault
}

func (this *items) Verify(cs *yyds.CS, d any) (errs []error) {
	data := d.(*output)
	//errs = append(errs, cs.ITypes.Parse("egg", data.Egg, ITypeEgg, 0)...)
	//errs = append(errs, cs.ITypes.Parse("eggTask", data.EggTask, ITypeEggTask, 0)...)

	errs = append(errs, cs.ITypes.Parse("unit", data.Unit, ITypeUnit, 0)...)
	errs = append(errs, cs.ITypes.Parse("Chip", data.Chip, ITypeChip, 0)...)
	//errs = append(errs, cs.ITypes.Parse("build", data.Build, ITypeBuild, 0)...)
	errs = append(errs, cs.ITypes.Parse("items", data.Item, ITypeItems, 0)...)

	//errs = append(errs, cs.ITypes.Parse("AchievementBall", data.AchievementBall, ITypeItems, 0)...)
	//errs = append(errs, cs.ITypes.Parse("AchievementPacket", data.AchievementPacket, ITypeItems, 0)...)
	//errs = append(errs, cs.ITypes.Parse("NickName", data.NickName, ITypeItems, 0)...)
	//errs = append(errs, cs.ITypes.Parse("Head", data.Head, ITypeItems, 0)...)
	//errs = append(errs, cs.ITypes.Parse("HeadFrame", data.HeadFrame, ITypeItems, 0)...)
	//errs = append(errs, cs.ITypes.Parse("BusinessCard", data.BusinessCard, ITypeItems, 0)...)
	//errs = append(errs, cs.ITypes.Parse("AppBackground", data.AppBackground, ITypeItems, 0)...)
	//errs = append(errs, cs.ITypes.Parse("PlayerCloth", data.PlayerCloth, ITypeItems, 0)...)

	errs = append(errs, cs.ITypes.Parse("UnitBase", data.UnitBase, ITypeUnitBase, 0)...)
	errs = append(errs, cs.ITypes.Parse("box", data.Box, ITypeBox, 0)...)

	//errs = append(errs, cs.ITypes.Parse("ItemGroup", data.ItemGroup, ITypeItemGroup, 0)...)
	//errs = append(errs, cs.ITypes.Parse("ItemPacks", data.ItemPacks, ITypeItemPacks, 0)...)
	//errs = append(errs, cs.ITypes.Parse("ItemPacks", data.ItemGroupBong, ITypeItemGroupBong, 0)...)

	//errs = append(errs, c.ITypes.Parse("role", data.Role, options.ITypeRole, 0)...)
	//errs = append(errs, c.ITypes.Parse("Goods", data.Goods, options.ITypeGoods, 0)...)
	//errs = append(errs, c.ITypes.Parse("Items", data.Items, 0, 0)...)
	//errs = append(errs, c.ITypes.Parse("Ticket", data.Ticket, options.ITypeTicket, 0)...)
	errs = append(errs, cs.ITypes.Parse("Order", data.Order, ITypeOrder, 0)...)
	//errs = append(errs, c.ITypes.Parse("hero", data.Hero, options.ITypeHero, 0)...)
	//errs = append(errs, c.ITypes.Parse("equip", data.Equip, options.ITypeEquip, 0)...)
	//errs = append(errs, c.ITypes.Parse("Task", data.Task, options.ITypeTask, 0)...)
	//errs = append(errs, c.ITypes.Parse("Active", data.Active, options.ITypeActive, 0)...)

	errs = append(errs, cs.ITypes.Parse("role", data.Role, ITypeRole, 0)...)
	errs = append(errs, cs.ITypes.Parse("GOODS", data.Goods, ITypeGoods, 0)...)
	errs = append(errs, cs.ITypes.Parse("ticket", data.Ticket, ITypeTicket, 0)...)
	errs = append(errs, cs.ITypes.Parse("Gacha", data.Gacha, ITypeGacha, 0)...)

	//errs = append(errs, cs.ITypes.Parse("AchievementBall", data.AchievementBall, ITypeAchievementBall, 0)...)
	//errs = append(errs, cs.ITypes.Parse("AchievementPacket", data.AchievementPacket, ITypeAchievementPacket, 0)...)
	//errs = append(errs, cs.ITypes.Parse("NickName", data.NickName, ITypeNickName, 0)...)
	//errs = append(errs, cs.ITypes.Parse("HeadIcon", data.Head, ITypeHeadIcon, 0)...)
	//errs = append(errs, cs.ITypes.Parse("HeadFrame", data.HeadFrame, ITypeHeadFrame, 0)...)
	//errs = append(errs, cs.ITypes.Parse("BusinessCard", data.BusinessCard, ITypeBusinessCard, 0)...)
	//errs = append(errs, cs.ITypes.Parse("AppBackground", data.AppBackground, ITypeAppBackground, 0)...)
	//errs = append(errs, cs.ITypes.Parse("PlayerCloth", data.PlayerCloth, ITypePlayerCloth, 0)...)

	cs.ITypes.Add(60100000, ITypeRole, 0, "经验")

	itemRandom := &ItemRandomProcess{
		Bong:  map[int32]*ItemBongRandom{},
		Packs: map[int32]*random.Random{},
		Group: map[int32]*random.Random{},
	}

	for _, c := range data.ItemPacks {
		w := itemRandom.Packs[c.Group]
		if w == nil {
			w = &random.Random{}
			itemRandom.Packs[c.Group] = w
		}
		w.Add(c.Id, c.Val)
	}
	for k, v := range itemRandom.Packs {
		v.Sort()
		cs.ITypes.Add(k, ITypeItemPacks, 0, "ItemPacks")
	}

	//物品组
	for _, c := range data.ItemGroup {
		w := itemRandom.Group[c.Group]
		if w == nil {
			w = &random.Random{}
			itemRandom.Group[c.Group] = w
		}
		w.Add(c.Id, c.Val)
	}
	for k, w := range itemRandom.Group {
		w.Sort()
		cs.ITypes.Add(k, ITypeItemGroup, 0, "ItemGroup")
	}

	//物品bong
	for _, c := range data.ItemGroupBong {
		w := itemRandom.Bong[c.Group]
		if w == nil {
			w = &ItemBongRandom{Type: c.Type}
			itemRandom.Bong[c.Group] = w
		}
		w.Add(c.Id, c.Val)
	}
	for k, w := range itemRandom.Bong {
		w.Sort()
		cs.ITypes.Add(k, ITypeItemGroupBong, 0, "ItemGroupBong")
	}

	cs.Process.Set(ItemRandomProcessName, itemRandom)
	return
}
